package com.hxh.wechat.adpter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.billy.android.swipe.SmartSwipe
import com.billy.android.swipe.SmartSwipeWrapper
import com.billy.android.swipe.SwipeConsumer
import com.billy.android.swipe.consumer.ShuttersConsumer
import com.billy.android.swipe.consumer.SlidingConsumer
import com.billy.android.swipe.listener.SimpleSwipeListener
import com.hxh.wechat.R
import com.hxh.wechat.activity.ActivityChat
import com.hxh.wechat.activity.ChatPageActivity
import com.hxh.wechat.activity.ContactActivity


class ContactAdapter(private val contactList: List<ContactItem>) :
    RecyclerView.Adapter<ContactAdapter.ViewHolder>() {
    class ContactItem {

        lateinit var friendUserId: String
        lateinit var contactImg: String//联系人的图片
        lateinit var contactName: String//联系人的名字
        lateinit var friendNickname: String

        constructor()
        constructor(
            friendUserId: String,
            contactImg: String,
            contactName: String,
            friendNickname: String
        ) {
            this.friendUserId = friendUserId
            this.contactImg = contactImg
            this.contactName = contactName
            this.friendNickname = friendNickname
        }

    }

    //这个消息是不是我发出的?
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val contactImg: ImageView = view.findViewById(R.id.contactImg)
        val friendNickname: TextView = view.findViewById(R.id.contactName)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        var inflater = LayoutInflater.from(parent.context)
        val view = inflater
            .inflate(R.layout.contacts_item, parent, false)

        var swipMenu = inflater.inflate(
            R.layout.contact_sideslip_menu,
            parent,
            false
        )
        var slidingConsumer = SlidingConsumer()

        //侧滑删除
        var swipeConsumer = SmartSwipe.wrap(view)
            .addConsumer(slidingConsumer)
            .setHorizontalDrawerView(swipMenu)
            .enableHorizontal() //启用左右两侧侧滑
        var viewHolder = ViewHolder(swipeConsumer.wrapper)

        //绑定事件
        view.setOnClickListener {
            val position = viewHolder.getAdapterPosition();
            val contact = contactList[position]
            ChatPageActivity.goChat(view.context, contact);
        }
        slidingConsumer.addListener(object : SimpleSwipeListener() {
            override fun onSwipeOpened(
                wrapper: SmartSwipeWrapper?,
                consumer: SwipeConsumer?,
                direction: Int
            ) {
                val position = viewHolder.getAdapterPosition();
                val contact = contactList[position]
                //聊天
                swipMenu.findViewById<TextView>(R.id.delete).setOnClickListener {
                    ContactActivity.contactList.remove(contact);
                    ContactActivity.notifyDataSetChanged();
                }

            }
        })



        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val contact = contactList[position]
        holder.contactImg.load(contact.contactImg)
        holder.friendNickname.text = contact.friendNickname
        // 给列表项添加点击事件处理逻辑
        holder.itemView.setOnClickListener {
            ChatPageActivity.goChat(holder.itemView.context, contact);
        }

    }

    override fun getItemCount() = contactList.size
}